//
//  UILableViewControl.swift
//  DemoUI
//
//  Created by 白瑾浩 on 2022/1/7.
//

import Foundation
import UIKit
//labtext文本
class UILableViewControl:UIViewController{
    var mUIImageView:UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        let label = UILabel( frame: CGRect(x: 20, y: 100, width: 200, height: 30))
        label.text = "我是小白"
        self.view.addSubview( label )
        self.view.backgroundColor = .green
        
        let label2 = UILabel(frame: CGRect(x:20,y:140,width: 200,height: 30))
        label2.text = "我是一个自定义控件"
        label2.font = UIFont.boldSystemFont(ofSize:  20 )
        label2.textColor = UIColor.red
        label2.shadowColor = UIColor.green
        label2.shadowOffset = CGSize(width: 10, height:10)
        label2.textAlignment = NSTextAlignment.center
        self.view.addSubview( label2 )
        
            
        let label3 = UILabel(frame: CGRect(x:20,y:180,width: 200,height: 100))
        label3.text = "我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文我是长文"
        label3.font = UIFont.boldSystemFont(ofSize:  20 )
        label3.textColor = UIColor.red
        label3.shadowColor = UIColor.green
        label3.shadowOffset = CGSize(width: 10, height:10)
        label3.textAlignment = NSTextAlignment.center
        label3.numberOfLines = 0
        label3.lineBreakMode = NSLineBreakMode.byWordWrapping
        
        self.view.addSubview( label3 )
        
        let label4  = UILabel(frame: CGRect(x:20,y:300,width: 200,height: 60))
        label4.numberOfLines = 4
        //label4.lineBreakMode = NSLineBreakMode.byWordWrapping
        label4.lineBreakMode = .byCharWrapping
        let attri = NSMutableAttributedString(string: "我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本我是个性化文本")
        attri.addAttributes([NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 20),NSAttributedString.Key.foregroundColor:UIColor.red], range: NSRange(location: 0, length: 2))
        attri.addAttributes([NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 16),NSAttributedString.Key.foregroundColor:UIColor.blue], range: NSRange(location: 3, length: 3))
        label4.attributedText = attri
        
        self.view.addSubview( label4 )
        
        
        //imageView
        
        let image1 =  UIImage(named: "音乐娱乐")
//        let image1 =  UIImage(contentsOfFile: "filePath")
//        let image1 =  UIImage(data: Data() )
        mUIImageView = UIImageView(image: image1)
        mUIImageView.frame = CGRect(x: 20, y: 400, width: 100, height: 100)
        
        
        self.view.addSubview( mUIImageView )
        
        
        
        let button33 = UIButton(type: UIButton.ButtonType.system)
        button33.frame = CGRect(x: 20, y: 550, width: 100, height: 30)
        button33.setTitle("stop", for: UIControl.State.normal)
        button33.addTarget(self, action: #selector(stop), for: .touchUpInside)
        self.view.addSubview( button33 )
        
        
        let button34 = UIButton(type: UIButton.ButtonType.system)
        button34.frame = CGRect(x: 200, y: 550, width: 100, height: 30)
        button34.setTitle("start", for: UIControl.State.normal)
        button34.addTarget(self, action: #selector(start), for: .touchUpInside)
        self.view.addSubview( button34 )
    
        
    }
    @objc func start(){
        rusumeRotateView()
    }
    @objc func stop(){
        stopRotateView()
    }
    
    /// 开始动画
    func startRotating() {
        let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation")
        rotateAnimation.isRemovedOnCompletion = false // 避免点击 Home 键返回,动画停止
        rotateAnimation.fromValue = 0.0
        rotateAnimation.toValue = Double.pi * 2
        rotateAnimation.duration = 20
        rotateAnimation.repeatCount = MAXFLOAT
        self.mUIImageView.layer.add(rotateAnimation, forKey: nil)
        isRotating = true
    }
    func stopRotateView(){
        if !isRotating {
            return
        }
        let pausedTime = self.mUIImageView.layer.convertTime(CACurrentMediaTime(), from: nil)
        // 让CALayer的时间停止走动
        self.mUIImageView.layer.speed = 0
        // 让CALayer的时间停留在pausedTime这个时刻
        self.mUIImageView.layer.timeOffset = pausedTime
        isRotating = false
    }
    var isRotating = false
    func rusumeRotateView(){
        if isRotating {
            return
        }
        if self.mUIImageView.layer.timeOffset == 0 {
            startRotating()
            return
        }
        let pausedTime = self.mUIImageView.layer.timeOffset
        // 1. 让CALayer的时间继续行走
        self.mUIImageView.layer.speed = 1.0
        // 2. 取消上次记录的停留时刻
        self.mUIImageView.layer.timeOffset = 0.01
        // 3. 取消上次设置的时间
        self.mUIImageView.layer.beginTime = 0.0
        // 4. 计算暂停的时间(这里也可以用CACurrentMediaTime()-pausedTime)
        let timeWhenpause = self.mUIImageView.layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
        // 5. 设置相对于父坐标系的开始时间(往后退timeSincePause)
        self.mUIImageView.layer.beginTime = timeWhenpause
        isRotating = true
    }
    
    
}
